//
//  RelationShipViewController.m
//  leo
//
//  Created by LIU YU LIN on 15/11/13.
//  Copyright © 2015年 mdx. All rights reserved.
//

#import "RelationShipViewController.h"
#import "SetTextViewController.h"
#import "LeoMsg.h"
#import "Tooler.h"
@interface RelationShipViewController ()

@end

@implementation RelationShipViewController

@synthesize origianlRelation;
@synthesize leftNavButton;
@synthesize rightNavButton;
@synthesize lastViewController;
@synthesize novaRelationShip;
int last=-1;
int now=0;
NSMutableArray *relationArray;
NSMutableArray *relationImageArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.title=@"请选择关系";
    self.navigationItem.rightBarButtonItem=rightNavButton;
    
    relationArray=[[NSMutableArray alloc]init];
    relationArray= [NSMutableArray arrayWithObjects:@"爸爸", @"妈妈",@"爷爷",@"奶奶",@"外公",@"外婆",@"哥哥",@"姐姐",@"自定义", nil];
    relationImageArray=[[NSMutableArray alloc]init];
    relationImageArray= [NSMutableArray arrayWithObjects:@"avater_man.png", @"avater_woman.png",@"avater_grandpa.png",@"avater_grandma.png",@"avater_grandpa.png",@"avater_grandma.png",@"avater_brother.png",@"avater_sister.png",@"avater_man.png", nil];

    
    [self initNavButton];
    [self initView];
    [self initLightButton];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)initNavButton {
    [leftNavButton setTarget:self];
    [leftNavButton setAction:@selector(back)];
    [rightNavButton setTarget:self];
    [rightNavButton setAction:@selector(save)];
}
- (void)save{
    if([origianlRelation isEqualToString:@""]||[origianlRelation length]<=0)
    {
        [LeoMsg showMsg:@"关系不能为空"];
        return;
        
    }
    else if([Tooler checkStringIllegal:origianlRelation ChnLength:6 EngLength:10]==1)
    {
        [LeoMsg showMsg:@"关系名称不合法"];
        return;
    }
    else if([Tooler checkStringIllegal:origianlRelation ChnLength:6 EngLength:10]==2)
    {
        [LeoMsg showMsg:@"关系名称太长，中文字符只能输入6个，英文字符最多10个"];
        return;
    }
    lastViewController.relationString=origianlRelation;
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)back{
    
    //NSLog(@"%@",origianlRelation);
    [self.navigationController popViewControllerAnimated:YES];
    //[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)initLightButton
{
    now=0;
    //NSMutableArray *relationArray= [NSMutableArray arrayWithObjects:@"爸爸", @"妈妈",@"爷爷",@"奶奶",@"外公",@"外婆",@"哥哥",@"姐姐",@"自定义", nil];
    for (int i=0;i<relationArray.count;i++)
    {
        NSString *rString=relationArray[i];
        if ([rString isEqualToString:origianlRelation])
        {
            now=i;origianlRelation=rString;
        }
    };
    [self lightAction];
}
-(void)lightAction
{
    if (last==now){return;}
    int a=now+10;
    int b=now+100;
    UIImageView *backView= [self.view viewWithTag:a];
    UIImageView *selectBackView= [self.view viewWithTag:b];
    backView.hidden=NO; selectBackView.hidden=NO;
    if (last<0){return;}
    a=last+10;
    b=last+100;
    backView= [self.view viewWithTag:a];
    selectBackView= [self.view viewWithTag:b];
    backView.hidden=YES; selectBackView.hidden=YES;
    
    //NSMutableArray *relationArray= [NSMutableArray arrayWithObjects:@"爸爸", @"妈妈",@"爷爷",@"奶奶",@"外公",@"外婆",@"哥哥",@"姐姐",@"自定义", nil];
    origianlRelation=relationArray[now];
    
    //NSLog(@"now is %d",now);
    //NSLog(@"arraycount is %d",relationArray.count);
    
    if (now==relationArray.count-1)
    {
        UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
        UIWindow *alertView=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        //
        SetTextViewController *nameVC=[[SetTextViewController alloc]init];
        //
        

        nameVC.initialTitle=@"请输入关系名称";
        nameVC.motherWindow=alertView;
        nameVC.type=2;
        nameVC.fatherVC=self;
        //[nameVC.confirmButton addTarget:self action:@selector(addRelationAction) forControlEvents:UIControlEventAllTouchEvents];
        alertView.rootViewController=nameVC;
        [keyWindow addSubview:alertView];
        [alertView setWindowLevel:UIWindowLevelAlert];
        [alertView makeKeyAndVisible];
        
        
//        if (![testLabel.text isEqual:@"123"])
//        {
//            [relationArray insertObject:testLabel.text atIndex:relationArray.count-1];
//            now=relationArray.count-1;
//            for(UIView *view in [self.view subviews])
//            {
//                [view removeFromSuperview];
//            };
//            [self initView];
//            [self lightAction];
//        }
    }
}

-(void)addRelationAction
{
    NSLog(@"又来这！");
    if (novaRelationShip==NULL)
    {
        return;
    }
    if ([novaRelationShip isEqual:@""])
    {
        return;
    }
    [relationArray insertObject:novaRelationShip atIndex:relationArray.count-1];
    [relationImageArray insertObject:@"avater_man.png" atIndex:relationArray.count-1];
    now=relationArray.count-2;
    for(UIView *view in [self.view subviews])
    {
        [view removeFromSuperview];
    };
    [self initView];
    [self lightAction];
}
-(void)initView
{
    float screenWidth=[UIScreen mainScreen].bounds.size.width;
    //NSMutableArray *relationArray= [NSMutableArray arrayWithObjects:@"爸爸", @"妈妈",@"爷爷",@"奶奶",@"外公",@"外婆",@"哥哥",@"姐姐",@"自定义", nil];
    float gap=10;//间隙
    float imageWidth=85;//头像宽度
    float labelWidth=60;//文字宽度
    float totalWidth=imageWidth+labelWidth;//总宽度
    float y=15;//上下间距
    
    for (int i=0;i<relationArray.count;i++)
    {
        float ox;float oy;
        if (i%2==0){ox=(screenWidth-totalWidth*2-gap)/2;}else{ox=(screenWidth-totalWidth*2-gap)/2+gap+totalWidth;};
        int z=i/2;
        oy=y+(z)*(y+85)+64;
        UIButton *relationButton=[[UIButton alloc]initWithFrame:CGRectMake(ox, oy, totalWidth, 85)];
        [relationButton addTarget:self action:@selector(relationAction:) forControlEvents:UIControlEventTouchDown];
        relationButton.tag=i+1000;
        UIView *containerView=[[UIView alloc]initWithFrame:CGRectMake(ox, oy, totalWidth, 85)];
        UIImageView *backView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 85, 85)];
        backView.backgroundColor=[UIColor whiteColor];
        backView.tag=i+10;
        UIImageView *selectBackView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 85, 85)];
        selectBackView.image=[UIImage imageNamed:@"avater_bg@.png"];
        selectBackView.tag=i+100;
        UIImageView *originalView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 75, 75)];
        //originalView.image=[UIImage imageNamed:@"avater_brother.png"];
        originalView.image=[UIImage imageNamed:relationImageArray[i]];
        UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(85+5, 35, labelWidth, 15)];
        nameLabel.text=relationArray[i];
        
        
        
        //正方形换圆形
        backView.layer.cornerRadius =backView.frame.size.width / 2;  backView.clipsToBounds = YES;
        selectBackView.layer.cornerRadius =selectBackView.frame.size.width / 2;  selectBackView.clipsToBounds = YES;
        originalView.layer.cornerRadius =originalView.frame.size.width / 2;  originalView.clipsToBounds = YES;
        
        
        backView.hidden=YES;
        selectBackView.hidden=YES;
        [containerView addSubview:backView];
        [containerView addSubview:selectBackView];
        [containerView addSubview:originalView];
        [containerView addSubview:nameLabel];
        //[containerView addSubview:relationButton];
        //containerView.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:containerView];
        [self.view addSubview:relationButton];
    }
    
    
    
}

-(void)relationAction:(id)sender
{
    UIButton *selectButton=(UIButton*)sender;
    int x=selectButton.tag-1000;
    last=now;
    now=x;
    [self lightAction];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
